package com.jinlejia.bbs.ui.mine;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.adapter.message.CommentAdapter;
import com.jinlejia.bbs.bean.CommentBean;
import com.jinlejia.bbs.databinding.BbsActivityCommentListBinding;
import com.jinlejia.bbs.help.CommonEvent;
import com.jinlejia.bbs.logic.CommentLogic;
import com.jinlejia.bbs.logic.message.MessageCommentLogic;
import com.jinlejia.bbs.logic.message.MessageLikeListLogic;
import com.jinlejia.bbs.widget.BBSToast;
import com.jinlejia.bbs.widget.mp3.AudioController;
import com.jinlejia.bbs.widget.recycler.PullInterface;

import java.util.ArrayList;
//我的评论
public class BBSCommentListActivity extends AppCompatActivity implements MessageCommentLogic.LogicCallBack, PullInterface.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener, MessageLikeListLogic.LogicCallBack {
    private BbsActivityCommentListBinding binding;
    private ArrayList<CommentBean> data;
    private MessageCommentLogic logic;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.bbs_activity_comment_list);
        binding.setCommonEvent(CommonEvent.obtain());
        data = new ArrayList<>();
        context = this;
        logic = new MessageCommentLogic(this,data,this);
        initView();
    }
    private AudioController audioController;
    private CommentAdapter adapter;
    private void initView() {
        audioController = new AudioController(this);
        binding.swipeRefreshLayout.setColorSchemeResources(R.color.holo_blue_bright, R.color.holo_green_light,
                R.color.holo_orange_light, R.color.holo_red_light);
        binding.swipeRefreshLayout.setOnRefreshListener(this);

        adapter = new CommentAdapter(this,audioController,binding.recyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAutoLoadMoreListener(this);
        binding.swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                binding.swipeRefreshLayout.setRefreshing(true);
                logic.refresh();
            }
        });
        binding.inputLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeIme();
                binding.inputLayout.setVisibility(View.GONE);
            }
        });
        binding.inputEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length()>0){
                    binding.sendButton.setVisibility(View.VISIBLE);
                }else {
                    binding.sendButton.setVisibility(View.GONE);
                }
            }
        });

        adapter.setListener(new CommentAdapter.CommentListener() {
            @Override
            public void onReplyComment(final String answerCode, final String commentCode, final String toName, final String toPin) {
                binding.inputLayout.setVisibility(View.VISIBLE);
                openIme();
                binding.sendButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CommentLogic logic = new CommentLogic(context);
                        CommentLogic.CommentLogicSource source = new CommentLogic.CommentLogicSource();
                        source.setAnswerCode(answerCode);
                        source.setCommentsContent(binding.inputEditText.getText().toString());
                        source.setToName(toName);
                        source.setToPin(toPin);
                        source.setCommentsCode(commentCode);
                        logic.submitMessage(source, new CommentLogic.CommentLogicCallBack() {
                            @Override
                            public void onResult(boolean result) {
                                if (result){
                                    BBSToast.showToast(context,"评论成功",1500);
                                    binding.inputLayout.setVisibility(View.GONE);
                                    binding.inputEditText.setText(null);
                                    binding.sendButton.setOnClickListener(null);
                                    onRefresh();
                                }
                                hideKeyboard();

                            }
                        });
                    }
                });
            }
        });
    }
    private void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm==null)return;
        if (imm.isActive() && getCurrentFocus() != null) {
            if (getCurrentFocus().getWindowToken() != null) {
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }
    @Override
    public void onResult(boolean hasNextPage) {
        adapter.initData(data);
        if (!hasNextPage){
            binding.recyclerView.setAutoLoadMoreListener(null);
        }else {
            binding.recyclerView.setAutoLoadMoreListener(this);
        }
        binding.recyclerView.getAdapter().notifyDataSetChanged();
        binding.swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onLoadMore() {
        binding.swipeRefreshLayout.setRefreshing(true);
        logic.load();
    }

    @Override
    public void onRefresh() {
        binding.swipeRefreshLayout.setRefreshing(true);
        logic.refresh();
    }

    @Override
    protected void onDestroy() {
        audioController.release();
        super.onDestroy();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            if (binding.inputLayout.getVisibility()==View.VISIBLE){
                binding.inputLayout.setVisibility(View.GONE);
                closeIme();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private void closeIme() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            if (binding.inputLayout.getVisibility()==View.VISIBLE){
                imm.hideSoftInputFromWindow(binding.inputEditText.getWindowToken(),0);
            }
        }
    }

    private void openIme() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        binding.inputEditText.requestFocus();
        if (imm != null){
            if (binding.inputLayout.getVisibility()==View.VISIBLE){
                imm.showSoftInput(binding.inputEditText, InputMethodManager.SHOW_FORCED);
            }
        }
    }
}
